import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
# 发信⽅的信息：发信邮箱，QQ 邮箱授权码
from_addr = '1936260727@qq.com'
password = 'eanbtiaahsfkefda'
# 收信⽅邮箱
to_addr = '1936260727@qq.com'
# 发信服务器
smtp_server = 'smtp.qq.com'
# 邮箱正⽂内容，第⼀个参数为内容，第⼆个参数为格式(plain 为纯⽂本)，第三个参数为编码
# msg = MIMEText('使⽤python发送邮件测试', 'plain', 'utf-8')
html_msg = """
宏软集团（内部绝密）
<p>Python 发送带附件的邮件测试...</p>
<p><a href="http://www.runoob.com">这是⼀个链接</a></p>
"""
msg = MIMEMultipart()
msg.attach(MIMEText(html_msg, 'html', 'utf-8'))
# 构造附件1，传送当前⽬录下的 test1.txt ⽂件
att1 = MIMEText(open('test1.txt', 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename="test1.txt"'
msg.attach(att1)
# 构造附件2，传送当前⽬录下的 test2.txt ⽂件
att2 = MIMEText(open('test2.txt', 'rb').read(), 'base64', 'utf-8')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename="test2.txt"'
msg.attach(att2)



# 邮件头信息
msg['From'] = Header('1936260727@qq.com')
msg['To'] = Header('1936260727@qq.com')
subject = 'Python SMTP 邮件测试'
msg['Subject'] = Header(subject, 'utf-8')
try:
    # 建⽴连接--qq邮箱服务和端⼝号（可百度查询）
    with smtplib.SMTP_SSL(smtp_server, 465) as smtpobj:
        # 登录--发送者账号和⼝令
        smtpobj.login(from_addr, password)
        # 发送邮件
        smtpobj.sendmail(from_addr, to_addr, msg.as_string())
        smtpobj.quit()
    print("邮件发送成功")
except Exception as e:
    print("⽆法发送邮件:", e)